﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_BillStream")]
    public class T_BillStream : IEntity<T_BillStream>
    {
        [Key]
        public int Id { get; set; }

        public int ManuscriptId { get; set; }

        /// <summary>
        /// 交易发起人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string Own { get; set; }

        /// <summary>
        /// 支付账串
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string DocStream { get; set; }
        /// <summary>
        /// 支付交易号
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Txid { get; set; }

        /// <summary>
        /// 支付总额
        /// </summary>
        public int TotalPay { get; set; }
        /// <summary>
        /// 支付状态
        /// 0:支付发起
        /// 1:支付完成
        /// </summary>
        public int Status { get; set; }

        [Column(TypeName="datetime")]
        public DateTime CreateTime { get; set; }
        
        [Column(TypeName = "datetime")]
        public DateTime CompleteTime { get; set; }

        /// <summary>
        /// 支付发起序列
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string PayToStream { get; set; }

        /// <summary>
        /// 支付返回序列
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(1024)]
        public string PayBackStream { get; set; }

        public string Message { get; set; }

        public override T_BillStream Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_BillStream Find(System.Linq.Expressions.Expression<Func<T_BillStream, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.BillStream.FirstOrDefault(where);
                if (item != null)
                {
                    this.CompleteTime = item.CompleteTime;
                    this.CreateTime = item.CreateTime;
                    this.DocStream = item.DocStream;
                    this.Id = item.Id;
                    this.ManuscriptId = item.ManuscriptId;
                    this.Message = item.Message;
                    this.PayBackStream = item.PayBackStream;
                    this.PayToStream = item.PayToStream;
                    this.Status = item.Status;
                    this.TotalPay = item.TotalPay;
                    this.Txid = item.Txid;
                    this.Own = item.Own;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_BillStream, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.BillStream.Any(where);
            }
        }
    }
}
